package com.cloudminds.nativeapplication.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import java.util.Set;

/**
 * @Description TODO
 * Created by sain on 2021/3/8 Monday.
 * Copyright © 2020 AISpeech. All rights reserved.
 * ================================================================================
 */
public class BleDeviceUtil {

    public static BluetoothDevice getTargetBondedBleDevice(Context context, String name) {
        if (TextUtils.isEmpty(name)) {
            return null;
        }

        Set<BluetoothDevice> bondedDevices = getBondedBleDevices(context);
        if (null != bondedDevices && bondedDevices.size() > 0) {
            for (BluetoothDevice bluetoothDevice : bondedDevices) {
                if (name.equals(bluetoothDevice.getName())) {
                    return bluetoothDevice;
                }
            }
        }
        return null;
    }

    public static Set<BluetoothDevice> getBondedBleDevices(@NonNull Context context) {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        if (null == bluetoothManager) {
            return null;
        }
        BluetoothAdapter adapter = bluetoothManager.getAdapter();
        return adapter.getBondedDevices();
    }
}
